package com.yk.i_wms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yk.i_wms.common.model.PageResult;
import com.yk.i_wms.common.model.Result;
import com.yk.i_wms.dto.SmsBarcodeDTO;
import com.yk.i_wms.model.SmsBarcode;
import com.yk.i_wms.service.ISmsBarcodeService;
import com.yk.i_wms.util.EasyPoiExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 
 *
 * @author hbq
 * @date 2021-04-02 13:29:50
 */
@Slf4j
@CrossOrigin
@RestController
@Api(tags = "条码")
@RequestMapping("sms")
public class SmsBarcodeController {
    @Autowired
    private ISmsBarcodeService smsBarcodeService;

    @ApiOperation(value = "条码生成")
    @PostMapping("/smsBarcode/createBarCode")
    public Result createBarCode(@RequestBody SmsBarcodeDTO smsBarcodeDTO) {
        return smsBarcodeService.createBarCode(smsBarcodeDTO);
    }
    @ApiOperation(value = "单个条码生成")
    @PostMapping("/smsBarcode/createSingleBarCode")
    public Result createSingleBarCode(@RequestBody Map<String, Object> params) {
        return smsBarcodeService.createSingleBarCode(params);
    }
    /**
     * 列表
     */
    @ApiOperation(value = "查询列表")
    @PostMapping("/smsBarcode/list")
    public Result<PageResult> list(@RequestBody Map<String, Object> params) {
        Page<Map> list= smsBarcodeService.findList(params);
        return Result.succeed(PageResult.restPage(list),"查询成功");
    }

    /**
     * 查询
     */
    @ApiOperation(value = "查询")
    @PostMapping("/smsBarcode/sel/{id}")
    public Result findUserById(@PathVariable Long id) {
        SmsBarcode model = smsBarcodeService.getById(id);
        return Result.succeed(model, "查询成功");
    }

    /**
     * 新增or更新
     */
    @ApiOperation(value = "新增or更新")
    @PostMapping("/smsBarcode/save")
    public Result save(@RequestBody SmsBarcode smsBarcode) {
        smsBarcodeService.saveOrUpdate(smsBarcode);
        return Result.succeed("保存成功");
    }

    /**
     * 批量新增or更新
     */
    @ApiOperation(value = "批量新增or更新")
    @PostMapping("/smsBarcode/saveBatch")
    public Result saveBatch(@RequestBody Map<String,List<SmsBarcode>> map) {
        List<SmsBarcode> models = map.get("models");
        if (models==null||models.isEmpty()){
            return Result.failed("请先生成条码");
        }
        String docNo = models.get(0).getDocNo();
        String itemCode = models.get(0).getItemCode();
        smsBarcodeService.remove(new LambdaQueryWrapper<SmsBarcode>()
                .eq(SmsBarcode::getDocNo,docNo)
                .eq(SmsBarcode::getItemCode,itemCode)
        );
        smsBarcodeService.saveBatch(models);
        return Result.succeed("保存成功");
    }

    /**
     * 删除
     */
    @ApiOperation(value = "批量删除")
    @PostMapping("/smsBarcode/del")
    public Result delete(@RequestBody Map<String,List<Long>> map) {
        List<Long> ids = map.get("ids");
        smsBarcodeService.removeByIds(ids);
        return Result.succeed("删除成功");
    }
    
    /**
     * 导入
     */
    @ApiOperation(value = "导入")
    @PostMapping("/smsBarcode/leadIn")
    public  Result leadIn(MultipartFile excel) throws Exception {
        int rowNum = 0;
        if (!excel.isEmpty()) {
            List<SmsBarcode> list = EasyPoiExcelUtil.importExcel(excel, 1, 1, SmsBarcode.class);
            rowNum = list.size();
            if (rowNum > 0) {
                list.forEach(u -> {
                        smsBarcodeService.save(u);
                });
                return Result.succeed("成功导入信息"+rowNum+"行数据");
            }
        }
        return Result.failed("导入失败");
    }
    
    /**
     * 导出（传入ids数组，选择指定id）
     */
    @ApiOperation(value = "导出（传入ids数组，选择指定id）")
    @PostMapping("/smsBarcode/leadOut")
    public void leadOut(@RequestBody Map<String,List<Long>> map,HttpServletResponse response) throws IOException {
        List<Long> ids = map.get("ids");
        List<SmsBarcode> smsBarcodeList = ids==null||ids.isEmpty()? smsBarcodeService.list(new QueryWrapper<>()):(List)smsBarcodeService.listByIds(ids);
        //导出操作
        EasyPoiExcelUtil.exportExcel(smsBarcodeList, "条码导出", "条码导出", SmsBarcode.class, "smsBarcode.xls", response);
    }
}
